<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 车辆
 *
 * @icon fa fa-car
 */
class Car extends Backend
{

    /**
     * Car模型对象
     * @var \app\admin\model\Car
     */
    protected $model = null;
    protected $relationSearch = true;
    protected $searchFields="id,plate,uid,carbrand.name,driver.username,user.username,user.nickname";
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Car;

    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['user'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $v) {
                $v->brand = (new \app\admin\model\CarBrand())->where('id', $v->brand_id)->value('name');
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }


    public function driver()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['user','driver','carbrand'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $v) {
                $v->brand = (new \app\admin\model\CarBrand())->where('id', $v->brand_id)->value('name');
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

}
